from datetime import datetime
from sqlalchemy import Column, Integer, String, DateTime, Text, Enum, ForeignKey, Boolean
from app.extensions import db


class MusicTemplate(db.Model):
    __tablename__ = 'music_templates'
    
    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
    template_name = Column(String(100), nullable=False)
    template_type = Column(Enum('hospital', 'school', 'enterprise', 'research'), default='hospital')
    category = Column(String(50), nullable=False)  # 一级菜单
    playlist_name = Column(String(100), nullable=False)  # 二级菜单
    play_mode = Column(Enum('sequence', 'single', 'random'), default='sequence')  # 播放模式
    remark = Column(Text)  # 备注
    is_default = Column(Boolean, default=False)  # 是否默认模板
    created_at = Column(DateTime, default=datetime.now)
    
    def to_dict(self):
        return {
            'id': self.id,
            'user_id': self.user_id,
            'template_name': self.template_name,
            'template_type': self.template_type,
            'category': self.category,
            'playlist_name': self.playlist_name,
            'play_mode': self.play_mode,
            'remark': self.remark,
            'is_default': self.is_default,
            'created_at': self.created_at.isoformat() if self.created_at else None
        }


class MusicFile(db.Model):
    __tablename__ = 'music_files'
    
    id = Column(Integer, primary_key=True)
    filename = Column(String(255), nullable=False)  # 原始文件名
    oss_key = Column(String(500), nullable=False)  # OSS存储key
    oss_url = Column(String(1000), nullable=False)  # OSS访问URL
    file_size = Column(Integer)  # 文件大小（字节）
    duration = Column(Integer)  # 音频时长（秒）
    category = Column(String(50))  # 分类
    playlist_name = Column(String(50))  # 播放列表
    created_at = Column(DateTime, default=datetime.now)
    
    def to_dict(self):
        return {
            'id': self.id,
            'filename': self.filename,
            'oss_key': self.oss_key,
            'oss_url': self.oss_url,
            'file_size': self.file_size,
            'duration': self.duration,
            'category': self.category,
            'playlist_name': self.playlist_name,
            'created_at': self.created_at.isoformat() if self.created_at else None
        }